﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.IO;
using System.Reflection;
using Microsoft.Win32;
using System.Diagnostics;
using System.Windows.Forms;


namespace SkynetCE.InstallLibrary
{
    [RunInstaller(true)]
    public partial class CustomInstaller : Installer
    {
        private const string INI_FILE = @"\Setup.ini";
        public CustomInstaller()
        {
            InitializeComponent();            
        }        
        private void CustomInstaller_AfterInstall(object sender, InstallEventArgs e)
        {
            var ceAppPath = GetWindowsCeApplicationManager();
            if (string.IsNullOrEmpty(ceAppPath ))
                return; 
            string iniPath = GetIniPath();                
            Process.Start(ceAppPath, iniPath);
        }
        private void CustomInstaller_AfterUninstall(object sender, InstallEventArgs e)
        {
            var ceAppPath  = GetWindowsCeApplicationManager();
            if (string.IsNullOrEmpty(ceAppPath))
                return;
            var iniPath = GetIniPath();                
            Process.Start(ceAppPath, String.Empty);
        }

        public string GetWindowsCeApplicationManager()
        {
            var ceAppPath = KeyExists();
            if ( string.IsNullOrEmpty(ceAppPath))
            {
                MessageBox.Show("Windows CE App Manager not installed", "Setup",MessageBoxButtons.OK, MessageBoxIcon.Error);                            
                return string.Empty; 
            }
            else
                return ceAppPath;
        }
        public string GetIniPath()
        {
            string dir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            return Path.Combine(dir,"Setup.ini");            
        }
        public string KeyExists()
        {
            RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\CEAPPMGR.EXE"); 
            if ( key == null) 
                return string.Empty;
            else
                return key.GetValue(string.Empty, string.Empty) as string;
        }
    
        

       
    }
}
